package com.dly.blog.domain.vo;

import com.dly.blog.domain.Menu;
import com.dly.blog.msg.R;
import com.dly.blog.utile.JsonUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Optional;

@Data
@Schema(name = "RouteVO", description = "路由信息")
public class RouteVO {

    @Schema(name = "component", description = "组件地址")
    private String component;

    @Schema(name = "name", description = "路由名称")
    private String name;

    @Schema(name = "path", description = "路由地址")
    private String path;

    @Schema(name = "meta", description = "元信息")
    private MetaVO meta;

    @Schema(name = "props", description = "开启路由参数props传递")
    private Boolean props;

    @Schema(name = "children", description = "子路由")
    private List<RouteVO> children;

    public static RouteVO build(Menu menu) {
        RouteVO routeVO = new RouteVO();
        routeVO.setComponent(menu.getComponent());
        routeVO.setName(menu.getRouteName());
        routeVO.setPath(menu.getRoutePath());
        routeVO.setProps(chickRouterParams(menu.getRoutePath()));

        MetaVO metaVO = new MetaVO();
        BeanUtils.copyProperties(menu,metaVO);
        Optional.ofNullable(menu.getQuery()).ifPresent(query-> metaVO.setQuery(JsonUtils.readFromJsonString(query, new TypeReference<>() {})));
        routeVO.setMeta(metaVO);
        return routeVO;
    }

    public static Boolean chickRouterParams(String path){
        if (StringUtils.isBlank(path)){
            return false;
        }
        return path.contains(":");
    }
}
